package csm.marc.extension;

/**
 * 
 * 
 * @author seungmun
 * @since 2006.1.18.
 */
public class UnsupportedCharsHandler
{
	private static UnsupportedCharsHandler handler = null;

	private final char asc183 = (char) 0x2502;
	private final char asc126 = (char) 0x2503;
	private final char asc192 = (char) 0x250c;
	private final char asc193 = (char) 0x250d;
	private final char asc194 = (char) 0x250e;
	private final char asc195 = (char) 0x250f;
	private final char asc196 = (char) 0x2510;
	private final char asc197 = (char) 0x2511;
	private final char asc198 = (char) 0x2512;
	private final char asc199 = (char) 0x2513;
	private final char asc200 = (char) 0x2514;
	private final char asc201 = (char) 0x2515;
	private final char asc202 = (char) 0x2516;
	private final char asc203 = (char) 0x2517;
	private final char asc204 = (char) 0x2518;
	private final char asc205 = (char) 0x2519;
	private final char asc206 = (char) 0x251a;
	private final char asc207 = (char) 0x251b;
	private final char asc208 = (char) 0x251c;
	private final char asc209 = (char) 0x251d;
	private final char asc210 = (char) 0x251e;
	private final char asc211 = (char) 0x251f;
	private final char asc212 = (char) 0x2520;
	private final char asc213 = (char) 0x2521;
	private final char asc214 = (char) 0x2522;
	private final char asc215 = (char) 0x2523;
	private final char asc216 = (char) 0x2524;
	private final char asc217 = (char) 0x2525;
	private final char asc218 = (char) 0x2526;
	private final char asc219 = (char) 0x2527;
	private final char asc220 = (char) 0x2528;
	private final char asc221 = (char) 0x2529;
	private final char asc222 = (char) 0x252a;
	private final char asc223 = (char) 0x252b;
	private final char asc224 = (char) 0x252c;
	private final char asc225 = (char) 0x252d;
	private final char asc226 = (char) 0x252e;
	private final char asc227 = (char) 0x252f;
	private final char asc228 = (char) 0x2530;
	private final char asc229 = (char) 0x2531;
	private final char asc230 = (char) 0x2532;
	private final char asc231 = (char) 0x2533;
	private final char asc232 = (char) 0x2534;
	private final char asc233 = (char) 0x2535;
	private final char asc234 = (char) 0x2536;
	private final char asc235 = (char) 0x2537;
	private final char asc236 = (char) 0x2538;
	private final char asc237 = (char) 0x2539;
	private final char asc238 = (char) 0x253a;
	private final char asc239 = (char) 0x253b;
	private final char asc240 = (char) 0x253c;
	private final char asc241 = (char) 0x253d;
	private final char asc242 = (char) 0x253e;
	private final char asc243 = (char) 0x253f;
	private final char asc244 = (char) 0x2540;
	private final char asc245 = (char) 0x2541;
	private final char asc246 = (char) 0x2542;
	private final char asc247 = (char) 0x2543;
	private final char asc248 = (char) 0x2544;
	private final char asc249 = (char) 0x2545;
	private final char asc250 = (char) 0x2546;
	private final char asc251 = (char) 0x2547;
	private final char asc252 = (char) 0x2548;
	private final char asc253 = (char) 0x2549;
	private final char asc254 = (char) 0x254a;
	private final char asc255 = (char) 0x254b;

	/**
	 * 지원되지 않는 문자열 처리기 인스턴스를 얻어옵니다.
	 * 
	 * @return
	 */
	public static UnsupportedCharsHandler getUnsupportedCharsHandler()
	{
		if (handler == null)
		{
			handler = new UnsupportedCharsHandler();
		}
		return handler;
	}

	/**
	 * ASCII에서 지원되지 않는 유니코드문자를 대체문자로 변환합니다.
	 * 
	 * @param unicode
	 *            유니코드 입력문자열
	 * @return 대체문자로 변환된 ASCII문자열
	 */
	public String toAscii(String unicode)
	{
		char[] ch = unicode.toCharArray();
		int len = ch.length;
		if (len <= 0) return unicode;

		for (int i = 0; i < len; i++)
		{

			if (ch[i] < '\u00c0' || ch[i] > '\u00ff')
			{
				if (ch[i] != '\u223c' && ch[i] != '\uff5e' && // '∼'
				ch[i] != '\u02dc' && ch[i] != '\u00b7' && ch[i] != '\u30fb') // 'ㆍ'
				continue;
			}

			switch (ch[i])
			{
				case '\u00b7':
				case '\u30fb':
					ch[i] = asc183;
					break;
				case '\u223c':
				case '\uff5e':
				case '\u02dc':
					ch[i] = asc126;
					break;
				case '\u00c0':
					ch[i] = asc192;
					break;
				case '\u00c1':
					ch[i] = asc193;
					break;
				case '\u00c2':
					ch[i] = asc194;
					break;
				case '\u00c3':
					ch[i] = asc195;
					break;
				case '\u00c4':
					ch[i] = asc196;
					break;
				case '\u00c5':
					ch[i] = asc197;
					break;
				case '\u00c6':
					ch[i] = asc198;
					break;
				case '\u00c7':
					ch[i] = asc199;
					break;
				case '\u00c8':
					ch[i] = asc200;
					break;
				case '\u00c9':
					ch[i] = asc201;
					break;
				case '\u00ca':
					ch[i] = asc202;
					break;
				case '\u00cb':
					ch[i] = asc203;
					break;
				case '\u00cc':
					ch[i] = asc204;
					break;
				case '\u00cd':
					ch[i] = asc205;
					break;
				case '\u00ce':
					ch[i] = asc206;
					break;
				case '\u00cf':
					ch[i] = asc207;
					break;
				case '\u00d0':
					ch[i] = asc208;
					break;
				case '\u00d1':
					ch[i] = asc209;
					break;
				case '\u00d2':
					ch[i] = asc210;
					break;
				case '\u00d3':
					ch[i] = asc211;
					break;
				case '\u00d4':
					ch[i] = asc212;
					break;
				case '\u00d5':
					ch[i] = asc213;
					break;
				case '\u00d6':
					ch[i] = asc214;
					break;
				case '\u00d7':
					ch[i] = asc215;
					break;
				case '\u00d8':
					ch[i] = asc216;
					break;
				case '\u00d9':
					ch[i] = asc217;
					break;
				case '\u00da':
					ch[i] = asc218;
					break;
				case '\u00db':
					ch[i] = asc219;
					break;
				case '\u00dc':
					ch[i] = asc220;
					break;
				case '\u00dd':
					ch[i] = asc221;
					break;
				case '\u00de':
					ch[i] = asc222;
					break;
				case '\u00df':
					ch[i] = asc223;
					break;
				case '\u00e0':
					ch[i] = asc224;
					break;
				case '\u00e1':
					ch[i] = asc225;
					break;
				case '\u00e2':
					ch[i] = asc226;
					break;
				case '\u00e3':
					ch[i] = asc227;
					break;
				case '\u00e4':
					ch[i] = asc228;
					break;
				case '\u00e5':
					ch[i] = asc229;
					break;
				case '\u00e6':
					ch[i] = asc230;
					break;
				case '\u00e7':
					ch[i] = asc231;
					break;
				case '\u00e8':
					ch[i] = asc232;
					break;
				case '\u00e9':
					ch[i] = asc233;
					break;
				case '\u00ea':
					ch[i] = asc234;
					break;
				case '\u00eb':
					ch[i] = asc235;
					break;
				case '\u00ec':
					ch[i] = asc236;
					break;
				case '\u00ed':
					ch[i] = asc237;
					break;
				case '\u00ee':
					ch[i] = asc238;
					break;
				case '\u00ef':
					ch[i] = asc239;
					break;
				case '\u00f0':
					ch[i] = asc240;
					break;
				case '\u00f1':
					ch[i] = asc241;
					break;
				case '\u00f2':
					ch[i] = asc242;
					break;
				case '\u00f3':
					ch[i] = asc243;
					break;
				case '\u00f4':
					ch[i] = asc244;
					break;
				case '\u00f5':
					ch[i] = asc245;
					break;
				case '\u00f6':
					ch[i] = asc246;
					break;
				case '\u00f7':
					ch[i] = asc247;
					break;
				case '\u00f8':
					ch[i] = asc248;
					break;
				case '\u00f9':
					ch[i] = asc249;
					break;
				case '\u00fa':
					ch[i] = asc250;
					break;
				case '\u00fb':
					ch[i] = asc251;
					break;
				case '\u00fc':
					ch[i] = asc252;
					break;
				case '\u00fd':
					ch[i] = asc253;
					break;
				case '\u00fe':
					ch[i] = asc254;
					break;
				case '\u00ff':
					ch[i] = asc255;
					break;
			}
		}
		return new String(ch);
	}

	/**
	 * ASCII에서 지원되지 않는 유니코드문자를 포함한 ASCII문자열을 원래 유니코드문자로 변환합니다.
	 * 
	 * @param ascii
	 *            ASCII 입력문자열
	 * @return 원래 유니코드 문자로 변환된 유니코드 문자열
	 */
	public String toUnicode(String ascii)
	{
		if (ascii == null) return ascii;

		char[] ch = ascii.toCharArray();
		int len = ch.length;
		if (len <= 0) return ascii;

		for (int i = 0; i < len; i++)
		{

			if (ch[i] < asc192 || ch[i] > asc255)
			{
				if (ch[i] != asc126 && ch[i] != asc183) continue;
			}

			switch (ch[i])
			{
				case asc183:
					ch[i] = '\u00b7';
					break;
				case asc126:
					ch[i] = '\u223c';
					break;
				case asc192:
					ch[i] = '\u00c0';
					break;
				case asc193:
					ch[i] = '\u00c1';
					break;
				case asc194:
					ch[i] = '\u00c2';
					break;
				case asc195:
					ch[i] = '\u00c3';
					break;
				case asc196:
					ch[i] = '\u00c4';
					break;
				case asc197:
					ch[i] = '\u00c5';
					break;
				case asc198:
					ch[i] = '\u00c6';
					break;
				case asc199:
					ch[i] = '\u00c7';
					break;
				case asc200:
					ch[i] = '\u00c8';
					break;
				case asc201:
					ch[i] = '\u00c9';
					break;
				case asc202:
					ch[i] = '\u00ca';
					break;
				case asc203:
					ch[i] = '\u00cb';
					break;
				case asc204:
					ch[i] = '\u00cc';
					break;
				case asc205:
					ch[i] = '\u00cd';
					break;
				case asc206:
					ch[i] = '\u00ce';
					break;
				case asc207:
					ch[i] = '\u00cf';
					break;
				case asc208:
					ch[i] = '\u00d0';
					break;
				case asc209:
					ch[i] = '\u00d1';
					break;
				case asc210:
					ch[i] = '\u00d2';
					break;
				case asc211:
					ch[i] = '\u00d3';
					break;
				case asc212:
					ch[i] = '\u00d4';
					break;
				case asc213:
					ch[i] = '\u00d5';
					break;
				case asc214:
					ch[i] = '\u00d6';
					break;
				case asc215:
					ch[i] = '\u00d7';
					break;
				case asc216:
					ch[i] = '\u00d8';
					break;
				case asc217:
					ch[i] = '\u00d9';
					break;
				case asc218:
					ch[i] = '\u00da';
					break;
				case asc219:
					ch[i] = '\u00db';
					break;
				case asc220:
					ch[i] = '\u00dc';
					break;
				case asc221:
					ch[i] = '\u00dd';
					break;
				case asc222:
					ch[i] = '\u00de';
					break;
				case asc223:
					ch[i] = '\u00df';
					break;
				case asc224:
					ch[i] = '\u00e0';
					break;
				case asc225:
					ch[i] = '\u00e1';
					break;
				case asc226:
					ch[i] = '\u00e2';
					break;
				case asc227:
					ch[i] = '\u00e3';
					break;
				case asc228:
					ch[i] = '\u00e4';
					break;
				case asc229:
					ch[i] = '\u00e5';
					break;
				case asc230:
					ch[i] = '\u00e6';
					break;
				case asc231:
					ch[i] = '\u00e7';
					break;
				case asc232:
					ch[i] = '\u00e8';
					break;
				case asc233:
					ch[i] = '\u00e9';
					break;
				case asc234:
					ch[i] = '\u00ea';
					break;
				case asc235:
					ch[i] = '\u00eb';
					break;
				case asc236:
					ch[i] = '\u00ec';
					break;
				case asc237:
					ch[i] = '\u00ed';
					break;
				case asc238:
					ch[i] = '\u00ee';
					break;
				case asc239:
					ch[i] = '\u00ef';
					break;
				case asc240:
					ch[i] = '\u00f0';
					break;
				case asc241:
					ch[i] = '\u00f1';
					break;
				case asc242:
					ch[i] = '\u00f2';
					break;
				case asc243:
					ch[i] = '\u00f3';
					break;
				case asc244:
					ch[i] = '\u00f4';
					break;
				case asc245:
					ch[i] = '\u00f5';
					break;
				case asc246:
					ch[i] = '\u00f6';
					break;
				case asc247:
					ch[i] = '\u00f7';
					break;
				case asc248:
					ch[i] = '\u00f8';
					break;
				case asc249:
					ch[i] = '\u00f9';
					break;
				case asc250:
					ch[i] = '\u00fa';
					break;
				case asc251:
					ch[i] = '\u00fb';
					break;
				case asc252:
					ch[i] = '\u00fc';
					break;
				case asc253:
					ch[i] = '\u00fd';
					break;
				case asc254:
					ch[i] = '\u00fe';
					break;
				case asc255:
					ch[i] = '\u00ff';
					break;
			}
		}
		return new String(ch);
	}

}
